/**********************************************************************
* Filename: CalendarDate.java
* Created: 2012/04/23
* @author Fernando Brito e Abreu
**********************************************************************/
package org.quasar.copaPaises.businessLayer;

import java.util.Set;
import java.util.HashSet;

public class CalendarDate
{
	public static Set<CalendarDate> allInstances = new HashSet<CalendarDate>();
	
	private int day;
	private int month;
	private int year;
	
	/**********************************************************************
	* Default constructor
	**********************************************************************/
	public CalendarDate()
	{
		allInstances.add(this);
	}
	
	/**********************************************************************
	* Parameterized constructor
	* @param day the day to initialize
	* @param month the month to initialize
	* @param year the year to initialize
	**********************************************************************/
	public CalendarDate(int day, int month, int year)
	{
		this.day = day;
		this.month = month;
		this.year = year;
		allInstances.add(this);
	} 
	
	/**********************************************************************
	* Standard attribute getter
	* @return the day of the calendarDate
	**********************************************************************/
	public int day()
	{
		return day;
	}
	
	/**********************************************************************
	* Standard attribute setter
	* @param day the day to set
	**********************************************************************/
	public void setDay(int day)
	{
		this.day = day;
	}
	
	/**********************************************************************
	* Standard attribute getter
	* @return the month of the calendarDate
	**********************************************************************/
	public int month()
	{
		return month;
	}
	
	/**********************************************************************
	* Standard attribute setter
	* @param month the month to set
	**********************************************************************/
	public void setMonth(int month)
	{
		this.month = month;
	}
	
	/**********************************************************************
	* Standard attribute getter
	* @return the year of the calendarDate
	**********************************************************************/
	public int year()
	{
		return year;
	}
	
	/**********************************************************************
	* Standard attribute setter
	* @param year the year to set
	**********************************************************************/
	public void setYear(int year)
	{
		this.year = year;
	}
	
	
	/**********************************************************************
	* User-defined operation specified in SOIL/OCL
	* @param date the date to set
	**********************************************************************/
	public void initS(String date)
	{
		this.year = Integer.parseInt(date.substring(0,4));
		this.month = Integer.parseInt(date.substring(5,7));
		this.day = Integer.parseInt(date.substring(8,10));
	}
	
	/**********************************************************************
	* User-defined operation specified in SOIL/OCL
	* @param t the t to set
	**********************************************************************/
	public boolean isAfter(CalendarDate t)
	{
		if (this.year > t.year)
			return true;
		if (this.year == t.year){
			if (this.month>t.month)
				return true;
			if(this.month == t.month){
				if(this.day>t.day)
					return true;
				else
					return false;
			}
			return false; 
		}
		return false;
	}
	
	/**********************************************************************
	* User-defined operation specified in SOIL/OCL
	* @param t the t to set
	**********************************************************************/
	public boolean isBefore(CalendarDate t)
	{
		if (this.year < t.year)
			return true;
		if (this.year == t.year){
			if (this.month<t.month)
				return true;
			if(this.month == t.month){
				if(this.day<t.day)
					return true;
				else
					return false;
			}
			return false;
		}
		return false;
	}
	
	/**********************************************************************
	* User-defined operation specified in SOIL/OCL
	* @param x the x to set
	* @param y the y to set
	**********************************************************************/
	public boolean isDivisible(int x, int y){
		return (((x / y)*y) == x);
	}
	
	/**********************************************************************
	* User-defined operation specified in SOIL/OCL
	* @param t the t to set
	**********************************************************************/
	public boolean isEqual(CalendarDate t)
	{
		return (this.year == t.year && this.month==t.month && this.day==t.day);
		
	}
	
	/**********************************************************************
	* User-defined operation specified in SOIL/OCL
	**********************************************************************/
	public boolean isLeap()
	{
		if (this.isDivisible(this.year, 400) || this.isDivisible(this.year, 4))
			return true;
		else
			return false;
	}
	
	/**********************************************************************
	* User-defined operation specified in SOIL/OCL
	* @param date the date to set
	**********************************************************************/
	public CalendarDate stringToDate(String date)
	{
		CalendarDate data = new CalendarDate();
		data.year = Integer.parseInt(date.substring(0,4));
		data.month = Integer.parseInt(date.substring(5,7));
		data.day = Integer.parseInt(date.substring(8,10));
		return data;
	}
	
	/**********************************************************************
	* User-defined operation specified in SOIL/OCL
	* @param t the t to set
	**********************************************************************/
	public int yearsSince(CalendarDate t)
	{
		if (this.month <  t.month)
			return (t.year - (this.year + 1));
		else
			if (this.month > t.month)
				return (t.year - this.year);
			else
				if(this.day < t.day)
					return (t.year - (this.year + 1));
				else
					return (t.year - this.year);
	}
	
	/* (non-Javadoc)
	* @see java.lang.Object#toString()
	*/
	@Override
	/**********************************************************************
	* Object serializer
	**********************************************************************/
	public String toString()
	{
		return "CalendarDate [day=" + day + ", month=" + month + ", year=" + year + "]\n";
	}
	
}
